﻿
//Author: 张亚涛
//createtime:2011-10-13 10:35:35
//Description:

using System;
using System.Data;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using Model;
using NS.DAL.Systems;


namespace DAL
{
    /// <summary>
    /// 该类提供了一系列操作 "CouponsTrade" 表的方法;
    /// </summary>
    public partial class CouponsTradeBase
    {
        const string SQL_Select = "select [TradeID],[OpenID],[SchemeID],[Money],[Quantity],[TradeNo],[PayNo],[PayDate],[State] from [CouponsTrade] ";
        #region .对本表的维护.
        #region select data
        /// <summary>
        /// 返回CouponsTrade表的所有数据 
        /// </summary>
        /// <returns></returns>        
        public static List<CouponsTrade> GetAllCouponsTrade()
        {
            List<CouponsTrade> couponsTradeList = new List<CouponsTrade>();
            SqlDataReader reader = null;

            string sql = SQL_Select;
            reader = SQLHelper.ExecuteDataReader(sql, null);
            while (reader.Read())
            {
                CouponsTrade couponsTrade = new CouponsTrade(reader);
                couponsTradeList.Add(couponsTrade);
            }
            reader.Close();
            return couponsTradeList;
        }
        /// <summary>
        /// 根据CouponsTrade表的TradeID字段返回数据 
        /// </summary>
        /// <param name="tradeID">TradeID</param>
        /// <returns></returns>       
        public static CouponsTrade GetCouponsTradeByTradeID(long tradeID)
        {
            CouponsTrade couponsTrade = null;
            SqlDataReader reader = null;

            string sql = SQL_Select + "where [TradeID] = @TradeID";
            SqlParameter[] paras = new SqlParameter[]{
            new SqlParameter("@TradeID",tradeID)
            };
            reader = SQLHelper.ExecuteDataReader(sql, paras);
            if (reader.Read())
            {
                couponsTrade = new CouponsTrade(reader);
            }
            reader.Close();
            return couponsTrade;
        }
        #endregion
        #region delete data
        /// <summary>
        /// 根据CouponsTrade表的TradeID字段删除数据 
        /// </summary>
        /// <param name="tradeID">tradeID</param>
        /// <returns></returns>        
        public static int DeleteCouponsTradeByTradeID(long tradeID)
        {
            string sql = "delete from [CouponsTrade] where [TradeID] = @TradeID";
            SqlParameter[] paras = new SqlParameter[]{
            new SqlParameter("@TradeID",tradeID)
            };
            return SQLHelper.ExecuteNonQuery(sql, paras);
        }
        #endregion
        #region update data
        /// <summary>
        /// prepare parameters 
        /// </summary>
        public static SqlParameter[] PrepareCommandParameters(CouponsTrade couponsTrade)
        {
            SqlParameter[] paras = new SqlParameter[]{
            new SqlParameter("@TradeID",couponsTrade.TradeID),
            new SqlParameter("@OpenID",couponsTrade.OpenID),
            new SqlParameter("@SchemeID",couponsTrade.SchemeID),
            new SqlParameter("@Money",couponsTrade.Money),
            new SqlParameter("@Quantity",couponsTrade.Quantity),
            new SqlParameter("@TradeNo",couponsTrade.TradeNo),
            new SqlParameter("@PayNo",couponsTrade.PayNo),
            new SqlParameter("@PayDate",couponsTrade.PayDate),
            new SqlParameter("@State",couponsTrade.State)    
            };
            return paras;
        }
        /// <summary>
        /// 根据CouponsTrade表的TradeID字段更新数据 
        /// </summary> 
        /// <param name="CouponsTrade">couponsTrade</param>
        /// <returns></returns>       
        public static int UpdateCouponsTradeByTradeID(CouponsTrade couponsTrade)
        {
            string sql = "update [CouponsTrade] set [OpenID] = @OpenID,[SchemeID] = @SchemeID,[Money] = @Money,[Quantity] = @Quantity,[TradeNo] = @TradeNo,[PayNo] = @PayNo,[PayDate] = @PayDate,[State] = @State where [TradeID] = @TradeID";
            SqlParameter[] paras = PrepareCommandParameters(couponsTrade);
            return SQLHelper.ExecuteNonQuery(sql, paras);
        }
        #endregion
        #region insert data
        /// <summary>
        /// 向CouponsTrade表插入一条数据，返回自增列数值，插入不成功则返回-1
        /// </summary>
        /// <param name="couponsTrade">CouponsTrade</param>       
        /// <returns></returns>        
        public static int AddCouponsTrade(CouponsTrade couponsTrade)
        {
            string sql = "insert into [CouponsTrade]([OpenID],[SchemeID],[Money],[Quantity],[TradeNo],[PayNo],[PayDate],[State])values(@OpenID,@SchemeID,@Money,@Quantity,@TradeNo,@PayNo,@PayDate,@State)    select @@identity";
            SqlParameter[] paras = PrepareCommandParameters(couponsTrade);
            int id = -1;

            string _ID = SQLHelper.ExecuteScalar(sql, paras).ToString();
            int.TryParse(_ID, out id);

            return id;
        }
        #endregion
        #endregion

        #region .SQL语句汇总.
        /// <summary>
        /// 返回CouponsTrade表的所有数据 （SQL语句）
        /// </summary>
        /// <returns></returns>        
        public static string GetAllCouponsTrade_SQL()
        {
            return SQL_Select;
        }
        #endregion
    }
}
